package com.yzh.oop9.oop9;

/**
 * 以面向对象的思想，编写自定义类描述图书信息。
 * 设定属性包括：书名，作者，出版社名，价格；方法包括：信息介绍
 * 要求：
 * 1) 设置属性的私有访问权限，通过公有的get,set方法实现对属性的访问
 * 2) 限定价格必须大于10，如果无效进行提示
 * 3) 设计构造方法实现对属性赋值
 * 4) 信息介绍方法描述图书所有信息
 * 5) 编写测试类，测试图书类的对象及相关方法（测试数据信息自定）
 */
public class Book  implements Comparable {
    private String bookName;//书名
    private String author; //作者
    private String publisher;//出版社名
    private double price; //价格


    @Override
    public int compareTo(Object obj) {
        Book other = (Book)obj;
        //比较价格
        //return (int)(this.price -other.price);
        if(this.price>other.price){
            return 1;
        }else if(this.price<other.price){
            return -1;
        }else{
            return 0;
        }

        //比较书名

        //如果书名相同，再比较价格
    }

    public Book(){

    }
    public Book(String bookName, String author, String publisher, double price){
        this.bookName =bookName;
        this.author = author;
        this.publisher = publisher;
        //this.price = price;
        this.setPrice(price);
    }

    public void setBookName(String bookName){
        this.bookName = bookName;
    }
    public String getBookName(){
        return bookName;
    }
    public void setAuthor(String author){
        this.author =  author;
    }
    public String getAuthor(){
        return this.author;
    }
    public void setPublisher(String publisher){
        this.publisher = publisher;
    }
    public String getPublisher(){
        return this.publisher;
    }
    public void setPrice(double price){
        if(price>10){
            this.price = price;
        }else{
            System.out.println("价格必须大于10元");
        }

    }
    public double getPrice(){
        return price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (Double.compare(book.price, price) != 0) return false;
        if (bookName != null ? !bookName.equals(book.bookName) : book.bookName != null) return false;
        if (author != null ? !author.equals(book.author) : book.author != null) return false;
        return publisher != null ? publisher.equals(book.publisher) : book.publisher == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = bookName != null ? bookName.hashCode() : 0;
        result = 31 * result + (author != null ? author.hashCode() : 0);
        result = 31 * result + (publisher != null ? publisher.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    /**
     * 信息介绍
     */
    public void show(){
        System.out.println("图书名称："+bookName);
        System.out.println("图书作者："+author);
        System.out.println("出版社："+publisher);
        System.out.println("图书价格："+price);
    }

}
